﻿using System.Text.RegularExpressions;

namespace MyRegexHelp
{
    /// <summary>
    /// 正则表达式帮助类
    /// </summary>
    public class MyRegexHelper
    {
        /// <summary>
        /// 判断是否IP
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 验证电话号码
        /// </summary>
        /// <param name="str_telephone">电话号码</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsTelephone(string str_telephone)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$");
        }

        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="str_Mobile">手机号码</param>
        /// <returns>校验结果是否符合规范</returns>        
        public static bool IsMobile(string str_Mobile)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_Mobile, @"^1[3-9]\\d{9}$");
        }

        /// <summary>
        /// 验证身份证号
        /// </summary>
        /// <param name="str_idcard">身份证号</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsIDcard(string str_idcard)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");
        }

        /// <summary>
        /// 验证输入为数字
        /// </summary>
        /// <param name="str_number">输入数字内容</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsNumber(string str_number)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$");
        }

        /// <summary>
        /// 验证邮编
        /// </summary>
        /// <param name="str_postalcode">邮编内容</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsPostalcode(string str_postalcode)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^\d{6}$");
        }

        /// <summary>
        /// 验证是否含有中文
        /// </summary>
        /// <param name="strChinese">校验内容</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsChineseCode(string strChinese)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strChinese, @"[\u4e00-\u9fa5]");
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="strPassword">校验内容</param>
        /// <returns>校验结果是否符合规范</returns>
        public static bool IsPassword(string strPassword)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(strPassword, @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z\\W]{8,12}$");
        }

    }
}
